package net.xdclass.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.config.RabbitMQConfig;
import net.xdclass.controller.request.wx.NativeOrderRequest;
import net.xdclass.enums.PayOrderType;
import net.xdclass.manager.WxPayManager;
import net.xdclass.model.WxOrderInfoDO;
import net.xdclass.model.WxOrderStatusDO;
import net.xdclass.model.message.PayOrderStatusEventMessage;
import net.xdclass.service.WxPayService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author Administrator
 * @description
 * @since 2023-07-02 22:09
 */
@Service
@Slf4j
public class WxPayServiceImpl implements WxPayService {

    @Autowired
    WxPayManager wxPayManager;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitMQConfig rabbitMQConfig;

    @Transactional
    @Override
    public Integer insertPrepareOrder(NativeOrderRequest nativeOrderRequest) {
        WxOrderInfoDO orderInfoDO = WxOrderInfoDO.builder()
                .appid(nativeOrderRequest.getAppid())
                .mchid(nativeOrderRequest.getMchid())
                .description(nativeOrderRequest.getDescription())
                .outTradeNo(nativeOrderRequest.getOut_trade_no())
                .timeExpire(LocalDateTime.parse(nativeOrderRequest.getTime_expire()))
                .attach(nativeOrderRequest.getAttach())
                .goodsTag(nativeOrderRequest.getGoods_tag())
                .supportFapiao(nativeOrderRequest.getSupport_fapiao() ? 1 : 0)
                .total(nativeOrderRequest.getMoneyAmount().getTotal())
                .currency(nativeOrderRequest.getMoneyAmount().getCurrency())
                .build();

        Integer orderRows = wxPayManager.insertPrepareOrder(orderInfoDO);
        log.info("插入订单行数: {}", orderRows);

        WxOrderStatusDO orderStatus = WxOrderStatusDO.builder()
                .orderId(orderInfoDO.getId())
                .orderStatus(PayOrderType.PREPARE.name())
                .build();
        Integer orderStatusRows = wxPayManager.insertOrderStatus(orderStatus);
        log.info("插入订单状态行数: {}", orderStatusRows);


        // 发送mq延迟消息
        PayOrderStatusEventMessage eventMessage = new PayOrderStatusEventMessage();
        eventMessage.setMchid(nativeOrderRequest.getMchid());
        eventMessage.setOutTradeNo(nativeOrderRequest.getOut_trade_no());
        eventMessage.setOrderId(orderStatus.getOrderId());
        log.info("发送延迟消息: exchange: {}, routingKey: {}, message: {}", rabbitMQConfig.getPayEventExchange(),
                rabbitMQConfig.getPayOrderStatusRoutingKey(),eventMessage);
        rabbitTemplate.convertAndSend(rabbitMQConfig.getPayEventExchange(),
                rabbitMQConfig.getPayOrderStatusRoutingKey(),eventMessage);

        return orderRows;
    }

    @Override
    public String queryOrderStatusByOutTradeNo(String outTradeNo, String merid) {
        return wxPayManager.queryOrderStatusByOutTradeNo(outTradeNo, merid);
    }

    @Override
    public Boolean changeOrderStatusByOutTradeNo(String outTradeNo, String merid,
                                                 PayOrderType oldStatus, PayOrderType newStatus) {

        return wxPayManager.changeOrderStatusByOutTradeNo(outTradeNo, merid, oldStatus, newStatus);
    }

    @Override
    public Boolean changeOrderStatusByOrderId(Long orderId, PayOrderType oldStatus, PayOrderType newStatus) {
        return wxPayManager.changeOrderStatusByOrderId(orderId, oldStatus, newStatus);
    }
}
